package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDTO;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品管理控制层
 */
@RestController
@RequestMapping("/dish")
@Api(tags = "菜品管理")
public class DishController {


    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     *
     * @param dto 菜品数据 和 口味数据
     * @return
     */
    @ApiOperation(value = "添加菜品")
    @PostMapping
    public R add(@RequestBody DishDTO dto) {
        return dishService.add(dto);
    }

    /**
     * 条件分页查询
     *
     * @return
     */
    @ApiOperation(value = "条件分页查询菜品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面条数", example = "10", required = true),
            @ApiImplicitParam(name = "name", value = "菜品名称", example = "红烧肉", required = false)
    })
    @GetMapping("/page")
    public R dishPage(Integer page, Integer pageSize, String name) {

        return dishService.dishPage(page, pageSize, name);
    }

    /**
     * 菜品数据回显
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "菜品数据回显")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id",value = "菜品ID",example = "1397851099502260226",required = true)
    public R findByWithFlavors(@PathVariable("id") Long id) {

        return dishService.findByWithFlavors(id);
    }

    /**
     * 修改菜品和口味
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改菜品和口味")
    @PutMapping
    public R updateDishWithFlavors(@RequestBody DishDTO dto) {

        return dishService.updateDishWithFlavors(dto);
    }

    /**
     * 批量删除菜品和口味
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除菜品和口味")
    @DeleteMapping
    @ApiImplicitParam(name = "ids",value = "批量菜品ID",example = "1397851099502260226,1397852391150759938",required = true)
    public R removeDishWithFlavors(Long[] ids) {

        return dishService.removeDishWithFlavors(ids);
    }


    /**
     * 批量修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量修改状态")
    @PostMapping("/status/{status}")
    public R updateDishStatus(@PathVariable int status, Long[] ids) {

        List<Dish> dishList = new ArrayList<>();

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setStatus(status);
            dish.setId(id);
            dishList.add(dish);
        }

        return dishService.updateDishStatus(dishList);
    }

    /**
     * 根据分类ID查询菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据分类ID查询菜品")
    @GetMapping("/list")
    public R findAllByCategoryId(DishDTO dto) {

        return dishService.findListAndFlavors(dto);
    }
}






